package ltd.dujiabao.dex.core.emitter;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import ltd.dujiabao.dex.core.ExecutorType;

/**
 * @author : DuJiabao
 * @Project : common-delay-executor
 * @Time : 2024/3/26 22:45
 * @Desc :
 */
public class EmitterFactoryHolder implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;
    private final Map<ExecutorType, EmitterFactory> factories = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        for (EmitterFactory factory : applicationContext.getBeansOfType(EmitterFactory.class).values()) {
            factories.put(factory.executorType(), factory);
        }
    }

    public EmitterFactory factory(ExecutorType executorType) {
        return factories.get(executorType);
    }
}
